#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>


#include "cJSON.h"
#include "external_sdk_client.h"

#define LICENSE_KEY	"xxxxxxxxxx"  
#define HTTP_PORT	80
#define IP_ADDR 	"192.168.1.168"

int EXTERNAL_SDK_Login(char *session_id)
{
	ExternalSdkClientLoginInfo login_info={0};
	
	snprintf(login_info.username, sizeof(login_info.username), "%s", "admin");
	snprintf(login_info.passwd, sizeof(login_info.passwd), "%s", "admin");
	snprintf(login_info.url, sizeof(login_info.url), "http://%s/api/session/login", IP_ADDR);
	snprintf(login_info.license_key, sizeof(login_info.license_key), "%s", LICENSE_KEY);
	login_info.port = HTTP_PORT;
	
	if(EXTERNAL_SDK_CLIENT_Login(&login_info, session_id) < 0)
	{
		printf("init fail\n");
		return -1;
	}

	printf("%s %d login success\n", __func__, __LINE__);

	return 0;
}


int EXTERNAL_SDK_GetParam(char *session_id)
{
	if(session_id == NULL)
    {
        printf("%s %d session_id is null\n", __func__, __LINE__);
        return -1;
    }	

	char *response = NULL;
	ExternalSdkClientCallParam call_param;

    cJSON *dataJson = cJSON_CreateObject();
	
	memset(&call_param, 0, sizeof(ExternalSdkClientCallParam));
	
	call_param.port = HTTP_PORT;
	snprintf(call_param.session_id, sizeof(call_param.session_id), "%s", session_id);
	snprintf(call_param.url, sizeof(call_param.url), "http://%s/api/system/device-info", IP_ADDR);

	cJSON_AddStringToObject(dataJson, "action", "get");
	cJSON_AddStringToObject(dataJson, "data", "");
	
    char *data_str  = cJSON_PrintUnformatted(dataJson);

	call_param.json_data = data_str;

	if(EXTERNAL_SDK_CLIENT_Call(&call_param, &response) < 0)
	{
		printf("%s %d EXTERNAL_SDK_CLIENT_Call fail", __func__, __LINE__);
		return -1;
	}

	printf("%s %d ret %s\n", __func__, __LINE__, response);

	cJSON_Delete(dataJson);
	free(data_str);
	free(response);
	response = NULL;
	
	return 0;
}


int main()
{
	char session_id[128] = {0};

	if(EXTERNAL_SDK_Login(session_id) < 0)
	{
		printf("login fail\n");
		return -1;
	}	

	printf("%s %d session_id %s\n", __func__, __LINE__, session_id);

	EXTERNAL_SDK_GetParam(session_id);

	while(1)
	{	
		sleep(10);
	}

	return 0;
}







          
